#!/usr/bin/env python

import os

try:
	dname = os.path.join ( "/tmp", "%s" % os.getuid () )
except:
	dname = os.path.join ( "/tmp", "liwe_tmp" )

try:
        os.mkdir ( dname )
except:
        pass

os.environ [ 'PYTHON_EGG_CACHE' ] = dname

from pyhpwsgi.impl.app import PyHPApp
from pyhpwsgi.impl.framework import PyHPFrameWork

def _handle_dir ( pyhp, res, environ ):
	import sys
	sys.stderr.write ( "HANDLE DIR!!" )

def _handle_unkn ( pyhp, res, environ ):
	environ [ 'pyhp_target_type' ] = "pyhp"
	environ [ 'pyhp_target_name' ] = "page.pyhp"
	return True

app = PyHPApp ()
application = PyHPFrameWork ( app )

if __name__ == '__main__':
	import sys
	from wsgiref.simple_server import make_server

	app.set_handler ( "dir", _handle_dir )

	try:
		port = int ( sys.argv [ 1 ] )
	except:
		port = 8000

	httpd = make_server ( '', port, application )
	print "Server UP on port %s" % port

	httpd.serve_forever ()
